<template>
  <div class="department-container">
    <div class="app-container">
      <page-tools title="共100条员工数据">
        <el-button
          round
          size="small"
          type="danger"
          @click="hImportExcel"
        >普通excel导出</el-button>
        <el-button
          round
          size="small"
          type="success"
        >复杂表头excel导出</el-button>
        <el-button
          round
          size="small"
          type="info"
          @click="$router.push('/import')"
        >excel导入</el-button>
        <el-button
          round
          size="small"
          type="primary"
          @click="addRole"
        >新增员工</el-button>
      </page-tools>
      <br>
      <el-card style="margin-top: 10px">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" sortable prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template slot-scope="{ row }">
              {{ format(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column sortable label="入职时间" prop="timeOfEntry" />
          <el-table-column label="账户状态" />
          <el-table-column label="操作" width="280">
            <template slot-scope="{ row }">
              <el-button
                type="text"
                size="small"
                @click="$router.push(`/employees/detail?id=${row.id}`)"
              >查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button
                type="text"
                size="small"
                @click="delRole(row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            :current-page="query.page"
            :page-sizes="[10, 15, 20, 25, 30]"
            :total="total"
            :page-size="query.size"
            layout="total,sizes,prev, pager, next"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>
      <!-- 弹框 -->
      <emp-dialog ref="empDilog" />
    </div>
  </div>
</template>
<script>
import { getEmployee, delEmployee } from '@/api/employees'
import EmployeeEnum from '@/constant/employees'
import EmpDialog from './EmpDialog.vue'
export default {
  name: 'Employees',
  components: { EmpDialog },
  data() {
    return {
      list: [],
      query: {
        page: 1,
        size: 10
      },
      total: 0
    }
  },
  created() {
    this.loadEmployee()
  },
  mounted() {},

  methods: {
    formatData(rows) {
      const enheader = Object.keys(rows[0])
      const map = {
        id: '编号',
        password: '密码',
        mobile: '手机号',
        username: '姓名',
        timeOfEntry: '入职日期',
        formOfEmployment: '聘用形式',
        correctionTime: '转正日期',
        workNumber: '工号',
        departmentName: '部门',
        staffPhoto: '头像地址'
      }
      const header = enheader.map((key) => map[key])
      const data = rows.map((item) => {
        item.formOfEmployment = this.format(item.formOfEmployment)
        return Object.values(item)
      })
      console.log(data, header)
      return { header, data }
    },
    // excel导出
    hImportExcel() {
      import('@/vendor/Export2Excel').then(async(excel) => {
        // excel表示导入的模块对象
        const res = await getEmployee({ page: 1, size: 10000 })
        const { header, data } = this.formatData(res.data.rows)
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    async addRole() {
      this.$refs.empDilog.isShow = true
    },
    // 删除
    async delRole(id) {
      await this.$confirm('确认删除吗？')
      await delEmployee(id)
      if (this.list.length === 1 && this.query.page > 1) {
        this.query.page--
      }
      this.loadEmployee()
      this.$message.success('删除成功')
    },

    handleCurrentChange(page) {
      this.query.page = page
      this.loadEmployee()
    },
    handleSizeChange(size) {
      this.query.size = size
      this.loadEmployee()
    },
    format(type) {
      const res = EmployeeEnum.hireType.find((item) => item.id === type)
      return res ? res.value : '未知'
    },
    async loadEmployee() {
      const { data } = await getEmployee(this.query)
      this.total = data.total
      this.list = data.rows
    }
  }
}
</script>
